package com.shd.edanpei.presenter;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.exception.ResponeThrowable;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.bean.MessageModel;
import com.shd.edanpei.bean.PagingModel;
import com.shd.edanpei.bean.TeamModel;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.presenter.contract.Team1Contract;
import com.shd.edanpei.util.LayoutUtils;
import com.socks.library.KLog;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/11/21.
 * yang-wen-wen
 */

public class Team1Presenter extends BasePresenterImpl<Team1Contract.Team1IView> implements Team1Contract.Team1IPresneter {

    private int currentPage;
    private int totalPage;

    public Team1Presenter(Team1Contract.Team1IView mView) {
        super(mView);
    }

    @Override
    public void refresh(final int teamID) {
        currentPage = 1;
        ApiCreator.getApi().teamList(currentPage, teamID, "")
                .compose(RxTransformer.<PagingModel<TeamModel>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PagingModel<TeamModel>>() {
                    @Override
                    protected void onSuccess(PagingModel<TeamModel> data) {
                        if (data != null && data.getList().size() > 0) {
                            totalPage = data.getTotalPage();
                            mBaseIView.getListAdapter().setNewData(data.getList());
                            if (currentPage >= totalPage) {
                                mBaseIView.getListAdapter().loadMoreEnd();
                            } else {
                                mBaseIView.getListAdapter().loadMoreComplete();
                            }
                            currentPage++;
                        } else {
                            mBaseIView.getListAdapter().setNewData(null);
//                            mBaseIView.getAdapter().setEmptyView(getNullLayout());
                            mBaseIView.getListAdapter().setEmptyView( LayoutUtils.getNullLayout(mBaseIView.getContext(), R.drawable.no_list_data, "暂无团队成员"));
                        }
                    }


                    @Override
                    public void onFailure(ResponeThrowable throwable) {
//                        mBaseIView.getAdapter().setEmptyView(getNullLayout());
                        mBaseIView.getListAdapter().setEmptyView( LayoutUtils.getNullLayout(mBaseIView.getContext(), R.drawable.no_list_data, "暂无团队成员"));
                    }

                });
    }

    @Override
    public void loadMore(final int teamID) {
        ApiCreator.getApi().teamList(currentPage, teamID, "")
                .compose(RxTransformer.<PagingModel<TeamModel>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PagingModel<TeamModel>>() {
                    @Override
                    protected void onSuccess(PagingModel<TeamModel> data) {
                        if (data != null && data.getList().size() > 0) {
                            mBaseIView.getListAdapter().addData(data.getList());
                            if (currentPage >= totalPage) {
                                mBaseIView.getListAdapter().loadMoreEnd();
                            } else {
                                mBaseIView.getListAdapter().loadMoreComplete();
                            }
                            currentPage++;
                        } else {
                            mBaseIView.getListAdapter().loadMoreEnd();
                        }
                    }

                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        mBaseIView.getListAdapter().loadMoreFail();
                    }
                });
    }

    @Override
    public void search(final int teamID, final String str) {
        ApiCreator.getApi().teamList(1, teamID, str)
                .compose(RxTransformer.<PagingModel<TeamModel>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PagingModel<TeamModel>>() {
                    @Override
                    protected void onSuccess(PagingModel<TeamModel> data) {
                        if (data != null && data.getList().size() > 0) {
                            totalPage = data.getTotalPage();
                            mBaseIView.getSearchAdapter().setNewData(data.getList());
                            if (currentPage >= totalPage) {
                                mBaseIView.getSearchAdapter().loadMoreEnd();
                            } else {
                                mBaseIView.getSearchAdapter().loadMoreComplete();
                            }
                            currentPage++;
                        } else {
                            mBaseIView.getSearchAdapter().setNewData(null);
                            mBaseIView.getSearchAdapter()
                                    .setEmptyView(LayoutUtils.getNullLayout(mBaseIView.getContext(), R.drawable.pic_noperson, "查无此人，请重新输入"));
                        }
                    }


                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        mBaseIView.getSearchAdapter()
                                .setEmptyView(LayoutUtils.getNullLayout(mBaseIView.getContext(), R.drawable.pic_noperson, "查无此人，请重新输入"));
                    }

                });
    }


}
